{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4.算数运算与数据对齐",
   "id": "1b11f2732cf60ada"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-12T07:24:24.586435Z",
     "start_time": "2025-09-12T07:24:24.583050Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "path = 'D:/2506A/monty03/day15/file/'"
   ],
   "id": "c40676a1546936b5",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 4.1 基本运算示例演示",
   "id": "9723200e670f597"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-12T07:26:30.889596Z",
     "start_time": "2025-09-12T07:26:30.880747Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建示例数据\n",
    "data = {\n",
    "    '1店': [2, 3, 4, 5, np.nan],\n",
    "    '2店': [1, -1, 0, 2, 3]\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "print(df)\n",
    "\n",
    "# 直接盐酸\n",
    "print(df['1店'] + df['2店']) # 任何数和NAN进行运算都是NAN\n",
    "\n",
    "print(df['1店'] / 0) # 整数除以0得inf\n",
    "print(df['2店'] / 0) # 负数除以0的到 -inf"
   ],
   "id": "fa0560f845c8e0e1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    1店  2店\n",
      "0  2.0   1\n",
      "1  3.0  -1\n",
      "2  4.0   0\n",
      "3  5.0   2\n",
      "4  NaN   3\n",
      "0    3.0\n",
      "1    2.0\n",
      "2    4.0\n",
      "3    7.0\n",
      "4    NaN\n",
      "dtype: float64\n",
      "0    inf\n",
      "1    inf\n",
      "2    inf\n",
      "3    inf\n",
      "4    NaN\n",
      "Name: 1店, dtype: float64\n",
      "0    inf\n",
      "1   -inf\n",
      "2    NaN\n",
      "3    inf\n",
      "4    inf\n",
      "Name: 2店, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 4.2 处理空值",
   "id": "b9f247e0ab2e1278"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-12T07:39:49.531952Z",
     "start_time": "2025-09-12T07:39:49.520635Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 空值填充为0\n",
    "df = pd.read_excel(path + '计算.xlsx')\n",
    "# print(df['1店'] + df['2店'] )  # 有空值参与运算结果为NAN\n",
    "\n",
    "# 将NAN填充0\n",
    "result = df['1店'].fillna(0) + df['2店'].fillna(0)\n",
    "\n",
    "print(result)"
   ],
   "id": "bfeac8e06e603002",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    8.0\n",
      "1    1.0\n",
      "2    1.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 4.3 使用方法处理空值",
   "id": "fefe777f4927a103"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-12T07:44:52.275637Z",
     "start_time": "2025-09-12T07:44:52.259998Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '计算.xlsx')\n",
    "print(df)\n",
    "\n",
    "result1 = df['1店'].add(df['2店'],fill_value=0)\n",
    "print(f'加法:{result1}')\n",
    "\n",
    "result2 = df['1店'].sub(df['2店'],fill_value=0)\n",
    "print(f'减法:{result2}')\n",
    "\n",
    "result3 = df['1店'].mul(df['2店'],fill_value=1)\n",
    "print(f'乘法:{result3}')\n",
    "\n",
    "result4 = df['1店'].div(df['2店'],fill_value=1).round(2)\n",
    "print(f'除法:{result4}')"
   ],
   "id": "92bad9492747256d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    1店   2店\n",
      "0  5.0  3.0\n",
      "1  NaN  1.0\n",
      "2  1.0  NaN\n",
      "加法:0    8.0\n",
      "1    1.0\n",
      "2    1.0\n",
      "dtype: float64\n",
      "减法:0    2.0\n",
      "1   -1.0\n",
      "2    1.0\n",
      "dtype: float64\n",
      "乘法:0    15.0\n",
      "1     1.0\n",
      "2     1.0\n",
      "dtype: float64\n",
      "除法:0    1.67\n",
      "1    1.00\n",
      "2    1.00\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 4.4 除法会产生无穷大",
   "id": "43d1ab6e7afe3ec9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-12T07:48:46.941136Z",
     "start_time": "2025-09-12T07:48:46.931880Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 演示无穷大的产生\n",
    "df = pd.read_excel(path + '无穷大.xlsx')\n",
    "print(df['1店'].div(df['2店'],fill_value=1)) # 除数为0产生无穷大"
   ],
   "id": "81fabec4a1b8967",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    inf\n",
      "1   -inf\n",
      "2    NaN\n",
      "3    NaN\n",
      "4    1.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-12T07:52:37.061593Z",
     "start_time": "2025-09-12T07:52:37.051879Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 无穷大的处理\n",
    "df = pd.read_excel(path + '无穷大.xlsx')\n",
    "\n",
    "## 将inf和-inf替换为NaN\n",
    "result = df['1店'].div(df['2店'])\n",
    "result.replace([np.inf,-np.inf],np.nan,inplace=True)\n",
    "# 将NAN填充为0\n",
    "result.fillna(0,inplace=True)\n",
    "print(result)"
   ],
   "id": "bc50af40a4985e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.0\n",
      "1    0.0\n",
      "2    0.0\n",
      "3    0.0\n",
      "4    1.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-12T07:54:33.633957Z",
     "start_time": "2025-09-12T07:54:33.623454Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def safe_division(series1, series2, fill_value=1, replace_inf=True):\n",
    "    \"\"\"\n",
    "    安全的除法运算，处理除零和无穷大问题\n",
    "    \"\"\"\n",
    "    result = series1.div(series2, fill_value=fill_value)\n",
    "    if replace_inf:\n",
    "        result = result.replace([np.inf, -np.inf], np.nan)\n",
    "    return result\n",
    "\n",
    "df = pd.read_excel(path + '无穷大.xlsx')\n",
    "result = safe_division(df['1店'],df['2店'])\n",
    "print(result)\n"
   ],
   "id": "7fa17a335de7d2a9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    NaN\n",
      "1    NaN\n",
      "2    NaN\n",
      "3    NaN\n",
      "4    1.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 4.5 数据对齐",
   "id": "9d96e8c9d6886141"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-12T08:01:50.798507Z",
     "start_time": "2025-09-12T08:01:50.777572Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df1 = pd.read_excel(path + '对齐.xlsx',sheet_name='Sheet1')\n",
    "df2 = pd.read_excel(path + '对齐.xlsx',sheet_name='Sheet2')\n",
    "print(f'df1：\\n{df1}')\n",
    "print(f'df2\\n{df2}')\n",
    "# 数据对齐运算, 相同的索引进行计算，不同的补NAN\n",
    "result1 = df1.add(df2)\n",
    "print(result1)\n",
    "\n",
    "# 使用fill_value填充未对齐的值\n",
    "result2 = df1.add(df2,fill_value=0)\n",
    "print(result2)\n"
   ],
   "id": "ab64bb9d31996335",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "df1：\n",
      "  序号    x    y    z\n",
      "0  a  100  101  102\n",
      "1  b  200  201  202\n",
      "df2\n",
      "  序号  y  z  t\n",
      "0  c  1  2  3\n",
      "1  b  4  2  5\n",
      "    t   x    y    z  序号\n",
      "0 NaN NaN  102  104  ac\n",
      "1 NaN NaN  205  204  bb\n",
      "     t      x    y    z  序号\n",
      "0  3.0  100.0  102  104  ac\n",
      "1  5.0  200.0  205  204  bb\n"
     ]
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 4.6 复杂运算中的对齐",
   "id": "bbd2ce3ddb97fbb2"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-12T08:08:12.569523Z",
     "start_time": "2025-09-12T08:08:12.561474Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 更复杂的数据对齐示例\n",
    "df3 = pd.DataFrame({\n",
    "    'A': [1, 2, 3],\n",
    "    'B': [4, 5, 6]\n",
    "}, index=['x', 'y', 'z'])\n",
    "\n",
    "df4 = pd.DataFrame({\n",
    "    'B': [10, 20],\n",
    "    'C': [30, 40]\n",
    "}, index=['y', 'z'])\n",
    "\n",
    "print(\"DataFrame 3:\")\n",
    "print(df3)\n",
    "print(\"\\nDataFrame 4:\")\n",
    "print(df4)\n",
    "\n",
    "\n",
    "# 加法运算\n",
    "result1 = df3.add(df4,fill_value=0)\n",
    "# result1.fillna(0, inplace=True)\n",
    "print(result1)"
   ],
   "id": "2334b797c6d4cf1e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DataFrame 3:\n",
      "   A  B\n",
      "x  1  4\n",
      "y  2  5\n",
      "z  3  6\n",
      "\n",
      "DataFrame 4:\n",
      "    B   C\n",
      "y  10  30\n",
      "z  20  40\n",
      "     A     B     C\n",
      "x  1.0   4.0   0.0\n",
      "y  2.0  15.0  30.0\n",
      "z  3.0  26.0  40.0\n"
     ]
    }
   ],
   "execution_count": 44
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
